package cn.doitedu.rtmk.demo7.rule_injector;

import org.roaringbitmap.longlong.Roaring64Bitmap;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class RuleMoni {
    public static void main(String[] args) throws IOException, SQLException {

        String ruleId = "rule-1-2";

        String ruleParamJson = "{\n" +
                "  \"rule_id\": \"rule-1-2\",\n" +
                "  \"static_profile\": [\n" +
                "    {\n" +
                "      \"tagName\": \"active_level\",\n" +
                "      \"tagValue\": [\n" +
                "        \"2\"\n" +
                "      ],\n" +
                "      \"compareType\": \">\"\n" +
                "    },\n" +
                "    {\n" +
                "      \"tagName\": \"age\",\n" +
                "      \"tagValue\": [\n" +
                "        \"25\",\n" +
                "        \"30\"\n" +
                "      ],\n" +
                "      \"compareType\": \"between\"\n" +
                "    }\n" +
                "  ],\n" +
                "  \"realtime_profile\": {\n" +
                "    \"eventId\": \"g\",\n" +
                "    \"eventCnt\": 2\n" +
                "  },\n" +
                "  \"fire_action\": \"u\"\n" +
                "}";

        // 假装从es查出来符合静态画像条件的人群有  1  3  5
        Roaring64Bitmap bitmap = Roaring64Bitmap.bitmapOf(1, 2, 5, 7, 9);

        ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
        DataOutputStream dataOut = new DataOutputStream(byteOut);
        bitmap.serialize(dataOut);
        // 规则的预圈选人群bitmap序列化字节
        byte[] bitmapBytes = byteOut.toByteArray();

        // 规则上线状态
        int onlineStatus = 1;

        // 规则的模型运算机代码






        Connection connection = DriverManager.getConnection("jdbc:mysql://doitedu:3306/doit40", "root", "root");

        PreparedStatement preparedStatement = connection.prepareStatement("insert into rule_meta values(?,?,?,?) ");

        preparedStatement.setString(1, ruleId);
        preparedStatement.setString(2, ruleParamJson);
        preparedStatement.setBytes(3, bitmapBytes);
        preparedStatement.setInt(4, onlineStatus);


        preparedStatement.execute();


        preparedStatement.close();
        connection.close();


    }
}
